//模块配置信息
require.config({
	//baseUrl: "./",
	paths: {
		//工具模块
		'less': 'tools/less.min',
		'jquery': 'tools/jquery.min',
		'bootstrap': 'tools/bootstrap.min',
		'handlebars': 'tools/handlebars',

		//配置模块
		'config': './config',
		"appConfig":"./appConfig",

		'top_bar': 'modules/top_bar',
		'article': 'modules/article',
		'showBigImg': 'modules/showBigImg',
		'userInfo': 'modules/userInfo',
		'city': 'modules/city',
		'modal_template': 'modules/modal_template',
		'cookie': 'modules/cookie',
		'check_log': 'modules/check_log',
		'login': 'modules/login',
		'other_log': 'modules/login',
		'reg': 'modules/reg',
		'user': 'modules/user',
	},
	skim: {  //配置依赖关系与接口
		"bootstrap": {	// bootstrap依赖jquery库
			deps: ['jquery'],
			exports: 'bootstrap'
		},
		"handlebars": {
			exports: 'Handlebars'
		},
		"user": {
			deps: ['modal_template']
		}
	}
});


//加载LESS, jquery, bootstrap
require(['less', 'jquery'], function(){
	require(['bootstrap']);	
});

//加载顶栏top_bar模块
require(['top_bar'], function(top_bar){
	top_bar();
});

//加载城市模板生成模块
require(['city'], function(city){
	city();
});

//加载中心问题模块，并用Hanlebars生成
require(['article', 'cookie'], function(article, cookie){
	// var date = new Date();
	// var timestamp = date.getTime();`

	//首次加载(sort, skip, total, clear, city, callback)------没有使用时间戳
	article.getArticles(0, 0, 10, true);

	//默认采用按时间加载，跳过首次加载的10
	var sorting = 0,
		skip = 10,
		theLast = false,
		city = '';

	//时间排序
	$(document).on('click', '.sort-by-time', function(){
		if (!$(this).hasClass('active')){
			$('.sort-by-heat').removeClass('active');
			$('.sort-by-time').addClass('active');
			article.getArticles(0, 0, 10, true, city, function(nums){
				sorting = 0;
				skip = 10;
				if (nums < 10){
					theLast = true;
				}
				else{
					theLast = false;
				}
			});
		}
	});
	//热度排序
	$(document).on('click', '.sort-by-heat', function(){
		if (!$(this).hasClass('active')){
			$('.sort-by-time').removeClass('active');
			$('.sort-by-heat').addClass('active');
			article.getArticles(1, 0, 10, true, city, function(nums){
				sorting = 1;
				skip = 10;
				if (nums < 10){
					theLast = true;
				}
				else{
					theLast = false;
				}
			});
		}
	});
	//城市排序
	$(document).on('click', '#cityModal .main-submit', function(){
		city = $('#cityModal .main-submit').attr('data-city');
		var isCountry = false;
		if (city == '全国'){
			city = '';
			isCountry = true;
		}
		article.getArticles(sorting, 0, 10, true, city, function(nums){
			skip = 10;
			if (nums < 10){
				theLast = true;
			}
			else{
				theLast = false;
			}

			if (isCountry){
				$('.center-problem .sorting-div .city span').text('全国');
			}
			else{
				if (city.length > 5){
					$('.center-problem .sorting-div .city span').text(city.substr(0, 5)+'..');
				}
				else{
					$('.center-problem .sorting-div .city span').text(city);
				}
			}
		});
	});

	//滚动到底部，加载更多
	var loading = false;
	$(window).scroll(function(){
		var document_height = $(document).height();
		var window_height = $(window).height();
		var scroll_top = $(document).scrollTop();

		if (scroll_top >= (document_height - window_height -50) && !loading && !theLast){
			loading = true;
			//根据全局sorting与skip，判断加载方式(clear = false)
			article.getArticles(sorting, skip, 10, false, city, function(nums){
				//回调中修改参数
				skip += 10;
				loading = false;
				if (nums <10){
					theLast = true;
				}
				else{
					theLast = false;
				}
			});
		}
	});

});

//加载右方listInfo个人信息
require(['userInfo'], function(userInfo){
	userInfo.listInfo();
});


